package com.nf.health.app.intervention_details.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Space;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.intervention_details.bean.ImproveDetailsBean;
import com.nf.health.app.utils.TextUtilTools;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 恩福 on 2016/11/14.
 * 危险因素控制目标及改善途径 Adapter
 *
 * @auther changpeng
 * @email obj_cp@126.com
 */

public class DangerImproveContentAdapter extends RecyclerView.Adapter {
    private static final int TYPE_HEAD_VIEW = 0;
    private static final int TYPE_CONTENT = 1;
    private static final int TYPE_SUGGEST = 2;
    private String mSuggest;
    private List<ImproveDetailsBean.ListBean> mListBean;
    private Context mContext;

    public DangerImproveContentAdapter(Context context, List<ImproveDetailsBean.ListBean> listBeen, String suggest) {
        this.mContext = context;
        this.mListBean = listBeen;
        this.mSuggest = suggest;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEAD_VIEW) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.scheme_common_text_subtitle, parent, false);
            return new HeadHolder(view);
        }
        if (viewType == TYPE_CONTENT) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.health_improve_item_solution_item, parent, false);
            return new ContentHolder(view);
        }
        if (viewType == TYPE_SUGGEST) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.layout_textview, parent, false);
            return new SuggestHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof ContentHolder) {
            ImproveDetailsBean.ListBean listBean = mListBean.get(position - 1);
            ContentHolder contentHolder = (ContentHolder) holder;
            String nowState = "目前状况&nbsp;&nbsp; <font color='#FF9F1A'>"+listBean.getStatus()+"</font>";
            String striveTarget = "努力目标&nbsp;&nbsp; <font color='#2E86F4'>"+listBean.getGoal()+"</font>";
            TextUtilTools.setTextFromHtml(contentHolder.tvNowState, nowState);
            TextUtilTools.setTextFromHtml(contentHolder.tvStriveTarget, striveTarget);
            contentHolder.tvDiagnose.setText(listBean.getDescribe());
            contentHolder.tvSubtitle.setText(listBean.getRealized());
            contentHolder.tvContent.setText(listBean.getMethod());
            if (position == getItemCount() - 2) { // 如果是最后一条则去掉下面的space
                contentHolder.space.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public int getItemCount() {
        return mListBean == null ? 0 : mListBean.size() + 1 + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_HEAD_VIEW;
        }
        if (position == getItemCount() - 1){
            return TYPE_SUGGEST;
        }
        return TYPE_CONTENT;
    }

    class HeadHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_subtitle)
        TextView tvSubTitle;

        public HeadHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            tvSubTitle.setText("危险因素控制目标及改善途径");
        }
    }

    class SuggestHolder extends RecyclerView.ViewHolder {

        public SuggestHolder(View itemView) {
            super(itemView);
            TextView textView = (TextView)itemView.findViewById(R.id.tv_cp);
            textView.setTextColor(mContext.getResources().getColor(R.color.orange));
            textView.setText(mSuggest);
        }
    }

    class ContentHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_now_state)
        TextView tvNowState;
        @Bind(R.id.tv_strive_target)
        TextView tvStriveTarget;
        @Bind(R.id.tv_danger_history)
        TextView tvDiagnose;
        @Bind(R.id.tv_subtitle)
        TextView tvSubtitle;
        @Bind(R.id.tv_content)
        TextView tvContent;
        @Bind(R.id.space)
        Space space;

        public ContentHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
